package com.noranj.web.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.noranj.web.shared.FolderItemDTO;

public class FolderList extends Composite {

	private static FolderListUiBinder uiBinder = GWT
			.create(FolderListUiBinder.class);

	interface FolderListUiBinder extends UiBinder<Widget, FolderList> {
	}

	@UiField
	VerticalPanel folderItemsPanel;
	private List<FolderItemDTO> folderItems;

	private final FolderManagementServiceAsync folderManagementService = GWT
			.create(FolderManagementService.class);

	public FolderList() {
		initWidget(uiBinder.createAndBindUi(this));
		getFolderItems();
	}

	private void setFolderItems(List<FolderItemDTO> folderItemDTOs) {
		this.folderItems = folderItemDTOs;
	}



	protected void setAndShowFolderItems(List<FolderItemDTO> folderItemDTOs) {
		for (FolderItemDTO folderItemDTO : folderItemDTOs) {
			folderItemsPanel.add(new FolderItem(folderItemDTO.getTitle()));
		}
	
	}

	public void getFolderItems() {
		folderManagementService
				.getFolderItems(new AsyncCallback<List<FolderItemDTO>>() {
					public void onFailure(Throwable caught) {
						Window.alert("An error occurred");
					}

					@Override
					public void onSuccess(List<FolderItemDTO> result) {
						setAndShowFolderItems(result);

					}

				});
	}
}
